<?php

namespace app\models;

use \app\components\BaseActiveRecord;
use app\helpers\AppHelper;

/**
 * This is the model class for table "{{%banner}}".
 *
 * @property integer $id
 * @property string $title
 * @property string $image
 * @property string $url
 * @property integer $sort
 * @property integer $status
 * @property integer $ctime
 */
class Banner extends BaseActiveRecord {

    public static $statusLabels = [
        self::STATUS_NO => '隐藏',
        self::STATUS_YES => '显示',
    ];

    public function init() {
        parent::init();
        $this->sort = 0;
        $this->status = static::STATUS_YES;
    }

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%banner}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['title', 'image'], 'required'],

            [['sort', 'status', 'ctime'], 'integer'],
            [['title', 'image'], 'string', 'max' => 120],
            [['url'], 'string', 'max' => 150],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'title' => '名称',
            'image' => '广告图',
            'url' => '地址',
            'sort' => '排序(数字越小越靠前)',
            'status' => '显示',
            'ctime' => 'Ctime',
        ];
    }

    public function fields() {
        $fields = parent::fields();
        $fields['image'] = function ($model) {
            return AppHelper::formatImage($model['image']);
        };
        unset($fields['sort'], $fields['ctime'], $fields['status']);
        return $fields;
    }
}
